using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Messenger.Models;
using Messenger.Services;

namespace Messenger.ViewModels;

[ObservableObject]
public partial class FunctionPageViewModel {
    public ObservableCollection<Poetry> Poetries { get; }

    public FunctionPageViewModel(
        IPoetryService poetryService) {
        var poetries = poetryService.GetPoetries();
        Poetries = new ObservableCollection<Poetry>();
        foreach (var poetry in poetries) {
            Poetries.Add(poetry);
        }

        WeakReferenceMessenger.Default
            .Register<PoetryAddedMessage>(this,
                (recipient, message) => 
                    Poetries.Add(message.Value));
    }
}